SpecialCasePropertyDrawerBase.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace ExternPropertyAttributes.Editor
  6. {
  7. public abstract class SpecialCasePropertyDrawerBase
  8. {
  9. public void OnGUI(Rect rect, SerializedProperty property)
  10. {
  11. // Check if visible
  12. bool visible = PropertyUtility.IsVisible(property);
  13. if (!visible)
  14. {
  15. return;
  16. }
  17. // Validate
  18. ValidatorAttribute[] validatorAttributes = PropertyUtility.GetAttributes<ValidatorAttribute>(property);
  19. foreach (var validatorAttribute in validatorAttributes)
  20. {
  21. validatorAttribute.GetValidator().ValidateProperty(property);
  22. }
  23. // Check if enabled and draw
  24. EditorGUI.BeginChangeCheck();
  25. bool enabled = PropertyUtility.IsEnabled(property);
  26. using (new EditorGUI.DisabledScope(disabled: !enabled))
  27. {
  28. OnGUI_Internal(rect, property, PropertyUtility.GetLabel(property));
  29. }
  30. // Call OnValueChanged callbacks
  31. if (EditorGUI.EndChangeCheck())
  32. {
  33. PropertyUtility.CallOnValueChangedCallbacks(property);
  34. }
  35. }
  36. public float GetPropertyHeight(SerializedProperty property)
  37. {
  38. return GetPropertyHeight_Internal(property);
  39. }
  40. protected abstract void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label);
  41. protected abstract float GetPropertyHeight_Internal(SerializedProperty property);
  42. }
  43. public static class SpecialCaseDrawerAttributeExtensions
  44. {
  45. private static Dictionary<Type, SpecialCasePropertyDrawerBase> _drawersByAttributeType;
  46. static SpecialCaseDrawerAttributeExtensions()
  47. {
  48. _drawersByAttributeType = new Dictionary<Type, SpecialCasePropertyDrawerBase>();
  49. }
  50. public static SpecialCasePropertyDrawerBase GetDrawer(this SpecialCaseDrawerAttribute attr)
  51. {
  52. SpecialCasePropertyDrawerBase drawer;
  53. if (_drawersByAttributeType.TryGetValue(attr.GetType(), out drawer))
  54. {
  55. return drawer;
  56. }
  57. else
  58. {
  59. return null;
  60. }
  61. }
  62. }
  63. }